package it.clariter.web.action;

import java.util.List;

import it.clariter.dao.AreaDAO;
import it.clariter.model.Area;
import it.clariter.web.form.AreaTipologiaVersoForm;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

public class CrudAreaAction extends DispatchAction {

	
	public ActionForward nuovo(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response)throws Exception{
		
		if(request.getSession().getAttribute("utente")==null){
			response.sendRedirect("login.jsp");
		}
		
		HttpSession session = request.getSession();
		request.setAttribute("titolo", "Inserisci nuova Area");
		session.setAttribute("value", "nuovaArea");
		session.setAttribute("indietro", "listaArea.jsp");
		session.setAttribute("tasto", "Inserisci nuovo");
		return mapping.findForward("insert");
		
	}
	
	public ActionForward modifica(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response)throws Exception{
		
		if(request.getSession().getAttribute("utente")==null){
			response.sendRedirect("login.jsp");
		}
		HttpSession session = request.getSession();
		AreaTipologiaVersoForm l = (AreaTipologiaVersoForm) form;
		
		Area area = new AreaDAO().get(l.getIdArea());
		request.setAttribute("oggetto", area);
		session.setAttribute("value", "modificaArea");
		request.setAttribute("titolo", "Modifica Area");
		session.setAttribute("tasto", "Modifica");
		session.setAttribute("indietro", "listaArea.jsp");
		
		return mapping.findForward("update");
		
	}
	
	public ActionForward cancella(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response)throws Exception{
		
		if(request.getSession().getAttribute("utente")==null){
			response.sendRedirect("login.jsp");
		}
		HttpSession session = request.getSession();
		AreaTipologiaVersoForm l = (AreaTipologiaVersoForm) form;

			
		new AreaDAO().delete(l.getIdArea());
		request.setAttribute("messaggio", "Cancellazione effettuata correttamente!");
		Area a = (Area) session.getAttribute("areaPerRicerca");
		List<Area> lista = new AreaDAO().findByExample(a);
		session.setAttribute("aree", lista);
			
		return mapping.findForward("delete");
		
	}
}
